package com.tomdxz.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tomdxz.model.User;
import com.tomdxz.service.LoginService;
import com.tomdxz.serviceimpl.LoginServiceImpl;

public class LoginController extends HttpServlet{
	
	LoginService lSer = new LoginServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		String uAccount = req.getParameter("account");
		String uPsw = req.getParameter("psw");	
		
		PrintWriter pw = resp.getWriter();
		HttpSession session = req.getSession();		
		
		User u = lSer.findUserByUaccountAndUpsw(uAccount, uPsw);
		if(u.getuId()>0){
			session.setAttribute("User", u);
			pw.write("1");
		}else{
			pw.write("0");		
		}		
		pw.flush();
		pw.close();
		
	}

}
